package com.coco.service.impl;

import com.coco.dao.ManagerMapper;
import com.coco.entity.MenuInfo;
import com.coco.model.MenuDetail;
import com.coco.service.ManagerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author GHOSTLaycoo
 * @date 2020/3/14 0014 - 下午 17:55
 */
@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public List<MenuDetail> selectMenu() {
        List<MenuInfo> menuInfos = managerMapper.selectMenu();
        List<MenuDetail> menuDetails = new ArrayList<>();
        menuInfos.forEach(s->{
            if(!StringUtils.isEmpty(s.getParent())){
                return;
            }
            MenuDetail menuDetail = new MenuDetail();
            if(StringUtils.isEmpty(s.getParent())){
                BeanUtils.copyProperties(s,menuDetail);
                menuDetail = menuChange(menuDetail, menuInfos, menuInfos.size());
            }
            menuDetails.add(menuDetail);
        });
        return menuDetails;
    }

    public MenuDetail menuChange(MenuDetail menuDetail,List<MenuInfo> menuInfos, int s){
        List<MenuDetail> lists = new ArrayList<>();
        for(int i = 0;i<menuInfos.size();i++){
            MenuDetail menuDetail1 = new MenuDetail();
            if(!StringUtils.isEmpty(menuInfos.get(i).getParent())){
                if(menuInfos.get(i).getParent().equals(menuDetail.getId())){
                    lists.add(MenuDetailChange(menuInfos.get(i),menuDetail1));
                }
            }
        }
        List<MenuDetail> ss = new ArrayList<>();
        if(lists.size()>0){
            for (MenuDetail list : lists) {
                ss.add(menuChange(list,menuInfos,s-1));
            }
        }
        menuDetail.setChildrenIds(ss);
        return menuDetail;
    }


    public MenuDetail MenuDetailChange(MenuInfo m,MenuDetail mm){
        BeanUtils.copyProperties(m,mm);
        return mm;
    }
}
